home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / glibmm-2.4 / glibmm / fileutils.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-20  |  16.0 KB  |  476 lines

  1. // -*- c++ -*-
  2. // Generated by gtkmmproc -- DO NOT MODIFY!
  3. #ifndef _GLIBMM_FILEUTILS_H
  4. #define _GLIBMM_FILEUTILS_H
  5.  
  6.  
  7. /* $Id: fileutils.hg,v 1.3 2004/01/22 18:38:12 murrayc Exp $ */
  8.  
  9. /* Copyright (C) 2002 The gtkmm Development Team
  10.  *
  11.  * This library is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU Library General Public
  13.  * License as published by the Free Software Foundation; either
  14.  * version 2 of the License, or (at your option) any later version.
  15.  *
  16.  * This library is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19.  * Library General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU Library General Public
  22.  * License along with this library; if not, write to the Free
  23.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26.  
  27. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  28. extern "C" { typedef struct _GDir GDir; }
  29. #endif
  30.  
  31. #include <iterator>
  32. #include <string>
  33.  
  34. #include <glibmmconfig.h>
  35. #include <glibmm/error.h>
  36.  
  37. GLIBMM_USING_STD(input_iterator_tag)
  38. GLIBMM_USING_STD(string)
  39.  
  40.  
  41. namespace Glib
  42. {
  43.  
  44. /** @addtogroup glibmmEnums Enums and Flags */
  45.  
  46. /**
  47.  * @ingroup glibmmEnums
  48.  * @par Bitwise operators:
  49.  * <tt>%FileTest operator|(FileTest, FileTest)</tt><br>
  50.  * <tt>%FileTest operator&(FileTest, FileTest)</tt><br>
  51.  * <tt>%FileTest operator^(FileTest, FileTest)</tt><br>
  52.  * <tt>%FileTest operator~(FileTest)</tt><br>
  53.  * <tt>%FileTest& operator|=(FileTest&, FileTest)</tt><br>
  54.  * <tt>%FileTest& operator&=(FileTest&, FileTest)</tt><br>
  55.  * <tt>%FileTest& operator^=(FileTest&, FileTest)</tt><br>
  56.  */
  57. enum FileTest
  58. {
  59.   FILE_TEST_IS_REGULAR = 1 << 0,
  60.   FILE_TEST_IS_SYMLINK = 1 << 1,
  61.   FILE_TEST_IS_DIR = 1 << 2,
  62.   FILE_TEST_IS_EXECUTABLE = 1 << 3,
  63.   FILE_TEST_EXISTS = 1 << 4
  64. };
  65.  
  66. /** @ingroup glibmmEnums */
  67. inline FileTest operator|(FileTest lhs, FileTest rhs)
  68.   { return static_cast<FileTest>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); }
  69.  
  70. /** @ingroup glibmmEnums */
  71. inline FileTest operator&(FileTest lhs, FileTest rhs)
  72.   { return static_cast<FileTest>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); }
  73.  
  74. /** @ingroup glibmmEnums */
  75. inline FileTest operator^(FileTest lhs, FileTest rhs)
  76.   { return static_cast<FileTest>(static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs)); }
  77.  
  78. /** @ingroup glibmmEnums */
  79. inline FileTest operator~(FileTest flags)
  80.   { return static_cast<FileTest>(~static_cast<unsigned>(flags)); }
  81.  
  82. /** @ingroup glibmmEnums */
  83. inline FileTest& operator|=(FileTest& lhs, FileTest rhs)
  84.   { return (lhs = static_cast<FileTest>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs))); }
  85.  
  86. /** @ingroup glibmmEnums */
  87. inline FileTest& operator&=(FileTest& lhs, FileTest rhs)
  88.   { return (lhs = static_cast<FileTest>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs))); }
  89.  
  90. /** @ingroup glibmmEnums */
  91. inline FileTest& operator^=(FileTest& lhs, FileTest rhs)
  92.   { return (lhs = static_cast<FileTest>(static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs))); }
  93.  
  94.  
  95. /** @defgroup FileUtils File Utilities
  96.  * Various file-related classes and functions.
  97.  */
  98.  
  99. /** Exception class for file-related errors.
  100.  * @ingroup FileUtils
  101.  */
  102. class FileError : public Glib::Error
  103. {
  104. public:
  105.   enum Code
  106.   {
  107.     EXISTS,
  108.     IS_DIRECTORY,
  109.     ACCESS_DENIED,
  110.     NAME_TOO_LONG,
  111.     NO_SUCH_ENTITY,
  112.     NOT_DIRECTORY,
  113.     NO_SUCH_DEVICE,
  114.     NOT_DEVICE,
  115.     READONLY_FILESYSTEM,
  116.     TEXT_FILE_BUSY,
  117.     FAULTY_ADDRESS,
  118.     SYMLINK_LOOP,
  119.     NO_SPACE_LEFT,
  120.     NOT_ENOUGH_MEMORY,
  121.     TOO_MANY_OPEN_FILES,
  122.     FILE_TABLE_OVERFLOW,
  123.     BAD_FILE_DESCRIPTOR,
  124.     INVALID_ARGUMENT,
  125.     BROKEN_PIPE,
  126.     TRYAGAIN,
  127.     INTERRUPTED,
  128.     IO_ERROR,
  129.     NOT_OWNER,
  130.     NOSYS,
  131.     FAILED
  132.   };
  133.  
  134.   FileError(Code error_code, const Glib::ustring& error_message);
  135.   explicit FileError(GError* gobject);
  136.   Code code() const;
  137.  
  138. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  139. private:
  140.   static void throw_func(GError* gobject);
  141.   friend void wrap_init(); // uses throw_func()
  142. #endif
  143. };
  144.  
  145.  
  146. /** @enum FileError::Code
  147.  * Values corresponding to <tt>errno</tt> codes returned from file operations
  148.  * on UNIX.
  149.  * Unlike <tt>errno</tt> codes, FileError::Code values are available on all
  150.  * systems, even Windows. The exact meaning of each code depends on what sort
  151.  * of file operation you were performing; the UNIX documentation gives more
  152.  * details.  The following error code descriptions come from the GNU C Library
  153.  * manual, and are under the copyright of that manual.
  154.  *
  155.  * It's not very portable to make detailed assumptions about exactly which
  156.  * errors will be returned from a given operation. Some errors don't occur on
  157.  * some systems, etc., sometimes there are subtle differences in when a system
  158.  * will report a given error, etc.
  159.  */
  160.  
  161. /** @var FileError::Code FileError::EXISTS
  162.  * <tt>(EEXIST)</tt> Operation not permitted; only the owner of the file (or
  163.  * other resource) or processes with special privileges can perform the operation.
  164.  * <br><br>
  165.  */
  166. /** @var FileError::Code FileError::IS_DIRECTORY
  167.  * <tt>(EISDIR)</tt> File is a directory; you cannot open a directory for writing,
  168.  * or create or remove hard links to it.
  169.  * <br><br>
  170.  */
  171. /** @var FileError::Code FileError::ACCESS_DENIED
  172.  * <tt>(EACCES)</tt> Permission denied; the file permissions do not allow the
  173.  * attempted operation.
  174.  * <br><br>
  175.  */
  176. /** @var FileError::Code FileError::NAME_TOO_LONG
  177.  * <tt>(ENAMETOOLONG)</tt> Filename too long.
  178.  * <br><br>
  179.  */
  180. /** @var FileError::Code FileError::NO_SUCH_ENTITY
  181.  * <tt>(ENOENT)</tt> No such file or directory.  This is a "file doesn't exist"
  182.  * error for ordinary files that are referenced in contexts where they are expected
  183.  * to already exist.
  184.  * <br><br>
  185.  */
  186. /** @var FileError::Code FileError::NOT_DIRECTORY
  187.  * <tt>(ENOTDIR)</tt> A file that isn't a directory was specified when a directory
  188.  * is required.
  189.  * <br><br>
  190.  */
  191. /** @var FileError::Code FileError::NO_SUCH_DEVICE
  192.  * <tt>(ENXIO)</tt> No such device or address.  The system tried to use the device
  193.  * represented by a file you specified, and it couldn't find the device. This can
  194.  * mean that the device file was installed incorrectly, or that the physical device
  195.  * is missing or not correctly attached to the computer.
  196.  * <br><br>
  197.  */
  198. /** @var FileError::Code FileError::NOT_DEVICE
  199.  * <tt>(ENODEV)</tt> This file is of a type that doesn't support mapping.
  200.  * <br><br>
  201.  */
  202. /** @var FileError::Code FileError::READONLY_FILESYSTEM
  203.  * <tt>(EROFS)</tt> The directory containing the new link can't be modified
  204.  * because it's on a read-only file system.
  205.  * <br><br>
  206.  */
  207. /** @var FileError::Code FileError::TEXT_FILE_BUSY
  208.  * <tt>(ETXTBSY)</tt> Text file busy.
  209.  * <br><br>
  210.  */
  211. /** @var FileError::Code FileError::FAULTY_ADDRESS
  212.  * <tt>(EFAULT)</tt> You passed in a pointer to bad memory.  (Glib won't
  213.  * reliably return this, don't pass in pointers to bad memory.)
  214.  * <br><br>
  215.  */
  216. /** @var FileError::Code FileError::SYMLINK_LOOP
  217.  * <tt>(ELOOP)</tt> Too many levels of symbolic links were encountered in
  218.  * looking up a file name.  This often indicates a cycle of symbolic links.
  219.  * <br><br>
  220.  */
  221. /** @var FileError::Code FileError::NO_SPACE_LEFT
  222.  * <tt>(ENOSPC)</tt> No space left on device; write operation on a file failed
  223.  * because the disk is full.
  224.  * <br><br>
  225.  */
  226. /** @var FileError::Code FileError::NOT_ENOUGH_MEMORY
  227.  * <tt>(ENOMEM)</tt> No memory available.  The system cannot allocate more
  228.  * virtual memory because its capacity is full.
  229.  * <br><br>
  230.  */
  231. /** @var FileError::Code FileError::TOO_MANY_OPEN_FILES
  232.  * <tt>(EMFILE)</tt> The current process has too many files open and can't
  233.  * open any more.  Duplicate descriptors do count toward this limit.
  234.  * <br><br>
  235.  */
  236. /** @var FileError::Code FileError::FILE_TABLE_OVERFLOW
  237.  * <tt>(ENFILE)</tt> There are too many distinct file openings in the
  238.  * entire system.
  239.  * <br><br>
  240.  */
  241. /** @var FileError::Code FileError::BAD_FILE_DESCRIPTOR
  242.  * <tt>(EBADF)</tt> Bad file descriptor; for example, I/O on a descriptor
  243.  * that has been closed or reading from a descriptor open only for writing
  244.  * (or vice versa).
  245.  * <br><br>
  246.  */
  247. /** @var FileError::Code FileError::INVALID_ARGUMENT
  248.  * <tt>(EINVAL)</tt> Invalid argument. This is used to indicate various kinds
  249.  * of problems with passing the wrong argument to a library function.
  250.  * <br><br>
  251.  */
  252. /** @var FileError::Code FileError::BROKEN_PIPE
  253.  * <tt>(EPIPE)</tt> Broken pipe; there is no process reading from the other
  254.  * end of a pipe.  Every library function that returns this error code also
  255.  * generates a <tt>SIGPIPE</tt> signal; this signal terminates the program
  256.  * if not handled or blocked.  Thus, your program will never actually see
  257.  * this code unless it has handled or blocked <tt>SIGPIPE</tt>.
  258.  * <br><br>
  259.  */
  260. /** @var FileError::Code FileError::TRYAGAIN
  261.  * <tt>(EAGAIN)</tt> Resource temporarily unavailable; the call might work
  262.  * if you try again later.
  263.  * We used TRYAGAIN instead of TRY_AGAIN, because that is a defined as a macro by a Unix header.
  264.  * <br><br>
  265.  */
  266. /** @var FileError::Code FileError::INTERRUPTED
  267.  * <tt>(EINTR)</tt> Interrupted function call; an asynchronous signal occurred
  268.  * and prevented completion of the call.  When this happens, you should try
  269.  * the call again.
  270.  * <br><br>
  271.  */
  272. /** @var FileError::Code FileError::IO_ERROR
  273.  * <tt>(EIO)</tt> Input/output error; usually used for physical read or write
  274.  * errors.  I.e. the disk or other physical device hardware is returning errors.
  275.  * <br><br>
  276.  */
  277. /** @var FileError::Code FileError::NOT_OWNER
  278.  * <tt>(EPERM)</tt> Operation not permitted; only the owner of the file (or other
  279.  * resource) or processes with special privileges can perform the operation.
  280.  * <br><br>
  281.  */
  282. /** @var FileError::Code FileError::FAILED
  283.  * Does not correspond to a UNIX error code; this is the standard "failed for
  284.  * unspecified reason" error code present in all Glib::Error error code
  285.  * enumerations.  Returned if no specific code applies.
  286.  */
  287.  
  288. class Dir;
  289.  
  290. /** The iterator type of Glib::Dir.
  291.  * @ingroup FileUtils
  292.  */
  293. class DirIterator
  294. {
  295. public:
  296.   typedef std::input_iterator_tag   iterator_category;
  297.   typedef std::string               value_type;
  298.   typedef int                       difference_type;
  299.   typedef value_type                reference;
  300.   typedef void                      pointer;
  301.  
  302.   DirIterator();
  303.  
  304. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  305.   DirIterator(GDir* gobject, const char* current);
  306. #endif
  307.  
  308.   std::string  operator*() const;
  309.   DirIterator& operator++();
  310.  
  311.   /** @note DirIterator has input iterator semantics, which means real
  312.    * postfix increment is impossible. The return type is @c void to
  313.    * prevent surprising behaviour.
  314.    */
  315.   void operator++(int);
  316.  
  317.   bool operator==(const DirIterator& rhs) const;
  318.   bool operator!=(const DirIterator& rhs) const;
  319.  
  320. private:
  321.   GDir*       gobject_;
  322.   const char* current_;
  323. };
  324.  
  325.  
  326. /** Utility class representing an open directory.
  327.  * @ingroup FileUtils
  328.  * It's highly recommended to use the iterator interface.  With iterators,
  329.  * reading an entire directory into a STL container is really easy:
  330.  * @code
  331.  * Glib::Dir dir (directory_path);
  332.  * std::list<std::string> entries (dir.begin(), dir.end());
  333.  * @endcode
  334.  * @note The encoding of the directory entries isn't necessarily UTF-8.
  335.  * Use Glib::filename_to_utf8() if you need to display them.
  336.  */
  337. class Dir
  338. {
  339. public:
  340.   typedef DirIterator iterator;
  341.   typedef DirIterator const_iterator;
  342.  
  343.   /** Opens a directory for reading. The names of the files in the
  344.    * directory can then be retrieved using read_name().
  345.    * @param path The path to the directory you are interested in.
  346.    * @throw Glib::FileError
  347.    */
  348.   explicit Dir(const std::string& path);
  349.  
  350. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  351.   explicit Dir(GDir* gobject);
  352. #endif
  353.  
  354.   /** Closes the directory and deallocates all related resources.
  355.    */
  356.   ~Dir();
  357.  
  358.   /** Retrieves the name of the next entry in the directory.
  359.    * The <tt>'.'</tt> and <tt>'..'</tt> entries are omitted.
  360.    * @return The entry's name or <tt>""</tt> if there are no more entries.
  361.    * @see begin(), end()
  362.    */
  363.   std::string read_name();
  364.  
  365.   /** Resets the directory.  The next call to
  366.    * read_name() will return the first entry again.
  367.    */
  368.   void rewind();
  369.  
  370.   /** Closes the directory and deallocates all related resources.
  371.    * Note that close() is implicitely called by ~Dir().  Thus you don't
  372.    * need to call close() yourself unless you want to close the directory
  373.    * before the destructor runs.
  374.    */
  375.   void close();
  376.  
  377.   /** Get the begin of an input iterator sequence.
  378.    * @return An input iterator pointing to the first directory entry.
  379.    */
  380.   DirIterator begin();
  381.  
  382.   /** Get the end of an input iterator sequence.
  383.    * @return An input iterator pointing behind the last directory entry.
  384.    */
  385.   DirIterator end();
  386.  
  387. private:
  388.   GDir* gobject_;
  389.  
  390.   // noncopyable
  391.   Dir(const Dir&);
  392.   Dir& operator=(const Dir&);
  393. };
  394.  
  395.  
  396. /** Returns @c true if any of the tests in the bitfield @a test are true.
  397.  * @ingroup FileUtils
  398.  * For example, <tt>(Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)</tt> will
  399.  * return @c true if the file exists; the check whether it's a directory
  400.  * doesn't matter since the existence test is true. With the current set of
  401.  * available tests, there's no point passing in more than one test at a time.
  402.  *
  403.  * Apart from <tt>Glib::FILE_TEST_IS_SYMLINK</tt> all tests follow symbolic
  404.  * links, so for a symbolic link to a regular file file_test() will return
  405.  * @c true for both <tt>Glib::FILE_TEST_IS_SYMLINK</tt> and
  406.  * <tt>Glib::FILE_TEST_IS_REGULAR</tt>.
  407.  *
  408.  * @note For a dangling symbolic link file_test() will return @c true for
  409.  * <tt>Glib::FILE_TEST_IS_SYMLINK</tt> and @c false for all other flags.
  410.  *
  411.  * @param filename A filename to test.
  412.  * @param test Bitfield of Glib::FileTest flags.
  413.  * @return Whether a test was true.
  414.  */
  415. bool file_test(const std::string& filename, FileTest test);
  416.  
  417. /** Opens a temporary file.
  418.  * @ingroup FileUtils
  419.  * See the %mkstemp() documentation on most UNIX-like systems. This is a
  420.  * portability wrapper, which simply calls %mkstemp() on systems that have
  421.  * it, and implements it in GLib otherwise.
  422.  * @param filename_template A string that should match the rules for
  423.  *   %mkstemp(), i.e. end in <tt>"XXXXXX"</tt>. The <tt>X</tt> string
  424.  *   will be modified to form the name of a file that didn't exist.
  425.  * @return A file handle (as from open()) to the file opened for reading
  426.  *   and writing. The file is opened in binary mode on platforms where there
  427.  *   is a difference. The file handle should be closed with close(). In
  428.  *   case of errors, <tt>-1</tt> is returned.
  429.  */
  430. int mkstemp(std::string& filename_template);
  431.  
  432. /** Opens a file for writing in the preferred directory for temporary files
  433.  * (as returned by Glib::get_tmp_dir()).
  434.  * @ingroup FileUtils
  435.  * @a prefix should a basename template; it'll be suffixed by 6 characters
  436.  * in order to form a unique filename.  No directory components are allowed.
  437.  *
  438.  * The actual name used is returned in @a name_used.
  439.  *
  440.  * @param prefix Template for file name, basename only.
  441.  * @retval name_used The actual name used.
  442.  * @return A file handle (as from <tt>open()</tt>) to the file opened for reading
  443.  * and writing. The file is opened in binary mode on platforms where there is a
  444.  * difference. The file handle should be closed with <tt>close()</tt>.
  445.  * @throw Glib::FileError
  446.  */
  447. int file_open_tmp(std::string& name_used, const std::string& prefix);
  448.  
  449. /** Opens a file for writing in the preferred directory for temporary files
  450.  * (as returned by Glib::get_tmp_dir()).
  451.  * @ingroup FileUtils
  452.  * This function works like file_open_tmp(std::string&, const std::string&)
  453.  * but uses a default basename prefix.
  454.  *
  455.  * @retval name_used The actual name used.
  456.  * @return A file handle (as from <tt>open()</tt>) to the file opened for reading
  457.  * and writing. The file is opened in binary mode on platforms where there is a
  458.  * difference. The file handle should be closed with <tt>close()</tt>.
  459.  * @throw Glib::FileError
  460.  */
  461. int file_open_tmp(std::string& name_used);
  462.  
  463. /** Reads an entire file into a string, with good error checking.
  464.  * @ingroup FileUtils
  465.  * @param filename A file to read contents from.
  466.  * @return The file contents.
  467.  * @throw Glib::FileError
  468.  */
  469. std::string file_get_contents(const std::string& filename);
  470.  
  471. } // namespace Glib
  472.  
  473.  
  474. #endif /* _GLIBMM_FILEUTILS_H */
  475.  
  476.